/*This .do file includes code for generating all the variables used in the analysis, merging the three datasets used, and the code for each regression conducted*/

/*generating lags and interaction terms from the original Boix dataset*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix.dta"sort imfcode yeartsset imfcode year
gen gini_lag=l1.aginiadjgen gini_lag_interact=gini_lag*demt1
gen agri_lag=l1.agri
gen agri_lag_interact=agri_lag*demt1gen cath_lag=l1.cathgen cath_lag_interact=cath_lag*demt1gen prot_lag=l1.protgen prot_lag_interact=prot_lag*demt1gen musl_lag=l1.muslgen musl_lag_interact=musl_lag*demt1gen ethdiv_lag=l1.ethdivgen ethdiv_lag_interact=ethdiv_lag*demt1gen relfract_lag=l1.relfractgen relfract_lag_interact=relfract_lag*demt1gen gdp1_lag=l1.rgdpch_dollars1985gen gdp2_lag=l2.rgdpch_dollars1985gen gdp3_lag=l3.rgdpch_dollars1985gen gdp4_lag=l4.rgdpch_dollars1985gen gdp5_lag=l5.rgdpch_dollars1985gen growthrate= (rgdpch_dollars1985-gdp1_lag)/gdp1_laggen growthrate_1lag= (gdp1_lag-gdp2_lag)/gdp2_laggen growthrate_2lag= (gdp2_lag-gdp3_lag)/gdp3_laggen growthrate_3lag= (gdp3_lag-gdp4_lag)/gdp4_laggen growthrate_4lag= (gdp4_lag-gdp5_lag)/gdp5_laggen movingavggrowthrate=(growthrate+ growthrate_1lag+ growthrate_2lag+ growthrate_3lag+ growthrate_4lag)/5gen movingavggrowthrate_lag=l1.movingavggrowthrategen growthrate_lag_interact=movingavggrowthrate_lag*demt1
save "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
*/
/*replicating the original regression in Boix, which appears in Table 3, middle columns*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
probit reg demt1 gini_lag gini_lag_interact agri_lag agri_lag_interact cath cath_lag_interact prot prot_lag_interact musl musl_lag_interact ethdiv ethdiv_lag_interact relfract relfract_lag_interact movingavggrowthrate_lag growthrate_lag_interact, r
*//*adding census data*/

/*
clear
use "/Users/hsoifer/Desktop/soifer census data.dta"drop if year>1999save "/Users/hsoifer/Desktop/soifer census data amended.dta"clearuse "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"merge 1:1 imfcode year using "/Users/hsoifer/Desktop/soifer census data amended.dta"rename censuslast10 c10gen c10xgini=c10*gini_lag
gen c10_lag_interact=c10*demt1
save "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta", replace
*/

/*re-running Boix analysis with census variables, Table 4 model 1*/

/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
probit reg demt1 gini_lag gini_lag_interact agri_lag agri_lag_interact cath cath_lag_interact prot prot_lag_interact musl musl_lag_interact ethdiv ethdiv_lag_interact relfract relfract_lag_interact movingavggrowthrate_lag growthrate_lag_interact c10 c10_lag_interact c10xgini, r
*//*cleaning up SIDD data and merging it*/
/*
clear
use "/Users/hsoifer/Desktop/SIDD dataset.dta"
/*Adding IMFcode to SIDD dataset for merging*/
/*
gen imfcode=1 if countryname=="Algeria"
replace imfcode=2 if countryname=="Angola"
replace imfcode=3 if countryname=="Benin"
replace imfcode=4 if countryname=="Botswana"
replace imfcode=5 if countryname=="Burkina Faso"
replace imfcode=6 if countryname=="Burundi" 
replace imfcode=7 if countryname=="Cameroon"
replace imfcode=8 if countryname=="Cape Verde"
replace imfcode=9 if countryname=="Central African Republic"
replace imfcode=10 if countryname=="Chad"
replace imfcode=11 if countryname=="Comoros"
replace imfcode=12 if countryname=="Congo"
replace imfcode=13 if countryname=="Djibouti"
replace imfcode=14 if countryname=="Egypt"
replace imfcode=15 if countryname=="Ethiopia"
replace imfcode=16 if countryname=="Gabon"
replace imfcode=17 if countryname=="Gambia"
replace imfcode=18 if countryname=="Ghana"
replace imfcode=19 if countryname=="Guinea"
replace imfcode=20 if countryname=="Guinea-Bissau"
replace imfcode=21 if countryname=="Ivory Coast"
replace imfcode=22 if countryname=="Kenya"
replace imfcode=23 if countryname=="Lesotho"
replace imfcode=24 if countryname=="Liberia"
replace imfcode=25 if countryname=="Malawi"
replace imfcode=25 if countryname=="Madagascar"
replace imfcode=26 if countryname=="Malawi"
replace imfcode=27 if countryname=="Mali"
replace imfcode=28 if countryname=="Mauritania"
replace imfcode=29 if countryname=="Mauritius"
replace imfcode=30 if countryname=="Morocco"
replace imfcode=31 if countryname=="Mozambique"
replace imfcode=32 if countryname=="Namibia"
replace imfcode=33 if countryname=="Niger"
replace imfcode=34 if countryname=="Nigeria"
replace imfcode=36 if countryname=="Rwanda"
replace imfcode=37 if countryname=="Senegal"
replace imfcode=38 if countryname=="Seychelles"
replace imfcode=39 if countryname=="Sierra Leone"
replace imfcode=40 if countryname=="Somalia"
replace imfcode=41 if countryname=="South Africa"
replace imfcode=42 if countryname=="Sudan"
replace imfcode=43 if countryname=="Swaziland"
replace imfcode=44 if countryname=="Tanzania"
replace imfcode=45 if countryname=="Togo"
replace imfcode=46 if countryname=="Tunisia"
replace imfcode=47 if countryname=="Uganda"
replace imfcode=48 if countryname=="Zaire"
replace imfcode=49 if countryname=="Zambia"
replace imfcode=50 if countryname=="Zimbabwe"
replace imfcode=51 if countryname=="Bahamas"
replace imfcode=52 if countryname=="Barbados"
replace imfcode=53 if countryname=="Belize"
replace imfcode=54 if countryname=="Canada"
replace imfcode=55 if countryname=="Costa Rica"
replace imfcode=56 if countryname=="Dominica"
replace imfcode=57 if countryname=="Dominican Republic"
replace imfcode=58 if countryname=="El Salvador"
replace imfcode=59 if countryname=="Grenada"
replace imfcode=60 if countryname=="Guatemala"
replace imfcode=61 if countryname=="Haiti"
replace imfcode=62 if countryname=="Honduras"
replace imfcode=63 if countryname=="Jamaica"
replace imfcode=64 if countryname=="Mexico"
replace imfcode=65 if countryname=="Nicaragua"
replace imfcode=66 if countryname=="Panama"
replace imfcode=67 if countryname=="Puerto Rico"
replace imfcode=68 if countryname=="St Kitts and Nevis"
replace imfcode=69 if countryname=="St Lucia"
replace imfcode=70 if countryname=="Trinidad and Tobago"
replace imfcode=72 if countryname=="United States"
replace imfcode=73 if countryname=="Argentina"
replace imfcode=74 if countryname=="Bolivia"
replace imfcode=75 if countryname=="Brazil"
replace imfcode=76 if countryname=="Chile"
replace imfcode=77 if countryname=="Colombia"
replace imfcode=78 if countryname=="Ecuador"
replace imfcode=79 if countryname=="Guyana"
replace imfcode=80 if countryname=="Paraguay"
replace imfcode=81 if countryname=="Peru"
replace imfcode=82 if countryname=="Suriname"
replace imfcode=83 if countryname=="Uruguay"
replace imfcode=84 if countryname=="Venezuela"
replace imfcode=85 if countryname=="Bahrain"
replace imfcode=86 if countryname=="Bangladesh"
replace imfcode=87 if countryname=="Bhutan"
replace imfcode=88 if countryname=="China"
replace imfcode=89 if countryname=="Hong Kong"
replace imfcode=90 if countryname=="India"
replace imfcode=91 if countryname=="Indonesia"
replace imfcode=92 if countryname=="Iran"
replace imfcode=93 if countryname=="Iraq"
replace imfcode=94 if countryname=="Israel"
replace imfcode=95 if countryname=="Japan"
replace imfcode=96 if countryname=="Jordan"
replace imfcode=97 if countryname=="South Korea"
replace imfcode=98 if countryname=="Kuwait"
replace imfcode=99 if countryname=="Laos"
replace imfcode=100 if countryname=="Malayasia"
replace imfcode=101 if countryname=="Mongolia"
replace imfcode=102 if countryname=="Myanmar"
replace imfcode=103 if countryname=="Nepal"
replace imfcode=104 if countryname=="Oman"
replace imfcode=105 if countryname=="Pakistan"
replace imfcode=106 if countryname=="Philippines"
replace imfcode=107 if countryname=="Qatar"
replace imfcode=108 if countryname=="Saudi Arabia"
replace imfcode=109 if countryname=="Singapore"
replace imfcode=110 if countryname=="Sri Lanka"
replace imfcode=113 if countryname=="Thailand"
replace imfcode=114 if countryname=="United Arab Emirates"
replace imfcode=115 if countryname=="Yemen"
replace imfcode=116 if countryname=="Austria"
replace imfcode=117 if countryname=="Belgium"
replace imfcode=118 if countryname=="Bulgaria"
replace imfcode=119 if countryname=="Cyprus"
replace imfcode=120 if countryname=="Czechoslovakia"
replace imfcode=121 if countryname=="Denmark"
replace imfcode=122 if countryname=="Finland"
replace imfcode=123 if countryname=="France"
replace imfcode=125 if countryname=="West Germany"
replace imfcode=126 if countryname=="Greece"
replace imfcode=127 if countryname=="Hungary"
replace imfcode=128 if countryname=="Iceland"
replace imfcode=129 if countryname=="Ireland"
replace imfcode=130 if countryname=="Italy"
replace imfcode=131 if countryname=="Luxembourg"
replace imfcode=132 if countryname=="Malta"
replace imfcode=133 if countryname=="Netherlands"
replace imfcode=134 if countryname=="Norway"
replace imfcode=135 if countryname=="Poland"
replace imfcode=136 if countryname=="Portugal"
replace imfcode=137 if countryname=="Romania"
replace imfcode=138 if countryname=="Spain"
replace imfcode=139 if countryname=="Sweden"
replace imfcode=140 if countryname=="Switzerland"
replace imfcode=141 if countryname=="Turkey"
replace imfcode=142 if countryname=="United Kingdom"
replace imfcode=144 if countryname=="Yugoslavia"
replace imfcode=145 if countryname=="Australia"
replace imfcode=146 if countryname=="Fiji"
replace imfcode=147 if countryname=="New Zealand"
replace imfcode=148 if countryname=="Papua New Guinea"
replace imfcode=149 if countryname=="Solomon Islands"
replace imfcode=150 if countryname=="Tonga"
replace imfcode=151 if countryname=="Vanuatu"
*/

/*dropping observations from countries not in Boix dataset*/
/*
drop if imfcode==.
save "/Users/hsoifer/Desktop/SIDD dataset amended.dta"
*/

/*merging SIDD data into main dataset*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
drop _merge
merge 1:1 imfcode year using "/Users/hsoifer/Desktop/SIDD dataset amended.dta"
rename ginibestestimateextrapolatedonly SIDD
save "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta", replace
*/
/*dropping extraneous variables added into main dataset from merged datasets*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
drop v5 giniestmiatedfromfitline standarderroroftheestimate cilowerlimit ciupperlimit insampleindicator1insample0outof degreeofbestpolynomialfitline v13 v14 v15 v16 v17 v18 _merge countryname censusinyear
*/

/*generating interaction terms for SIDD data*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
sort imfcode year
tsset imfcode year
gen SIDD_lag=l1.SIDD
gen SIDD_lag_interact=SIDD_lag*demt1
gen SIDDxcensus=SIDD*c10
save "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta", replace
*/
/*replicating Boix with SIDD data (Table 3 rightmost model)*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
probit reg demt1 SIDD_lag SIDD_lag_interact agri_lag agri_lag_interact cath cath_lag_interact prot prot_lag_interact musl musl_lag_interact ethdiv ethdiv_lag_interact relfract relfract_lag_interact movingavggrowthrate growthrate_lag_interact, r
*/

/*census analysis with SIDD data (Table 4 rightmost model)*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
probit reg demt1 SIDD SIDD_lag_interact agri_lag agri_lag_interact cath cath_lag_interact prot prot_lag_interact musl musl_lag_interact ethdiv ethdiv_lag_interact relfract relfract_lag_interact movingavggrowthrate growthrate_lag_interact c10 c10_lag_interact SIDDxcensus, r
*/

/*split sample analysis (from Web Appendix)*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
sort c10
by c10: probit reg demt1 SIDD SIDD_lag_interact agri_lag agri_lag_interact cath cath_lag_interact prot prot_lag_interact musl musl_lag_interact ethdiv ethdiv_lag_interact relfract relfract_lag_interact movingavggrowthrate_lag growthrate_lag_interact, r
*/

/*BTSCS analysis (from Web Appendix)*/
/*generating spell variable*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
btscs reg year imfcode, g(lastreg) nspline(3)
save "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta", replace
*/
/*lastreg is the spell variable, i.e. number of years since last regime change. _spline1 _spline2 _spline3 are the three splines*/
/*BTSCS analysis with SIDD data*/
/*
clear
use "/Users/hsoifer/Desktop/regime data from Boix with lags and interactions.dta"
probit reg SIDD c10 SIDDxcensus lastreg _spline1 _spline2 _spline3 agri cath prot musl ethdiv relfract movingavggrowthrate, robust
*/


